/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.common.utils;

import io.renren.common.validator.group.AliyunGroup;
import io.renren.common.validator.group.QcloudGroup;
import io.renren.common.validator.group.QiniuGroup;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * 常量
 *
 * @author Mark sunlightcs@gmail.com
 */
public class Constant {
    /**
     * 超级管理员ID
     */
    public static final int SUPER_ADMIN = 1;
    /**
     * 当前页码
     */
    public static final String PAGE = "page";
    /**
     * 每页显示记录数
     */
    public static final String LIMIT = "limit";
    /**
     * 排序字段
     */
    public static final String ORDER_FIELD = "sidx";
    /**
     * 排序方式
     */
    public static final String ORDER = "order";
    /**
     * 升序
     */
    public static final String ASC = "asc";

    /**
     * 登录类型
     */
    public static final String ADMIN_LOGIN = "adminLogin";
    public static final String OWNER_LOGIN = "ownerLogin";

    /**
     * 滞纳金利息
     */
    public static final BigDecimal RATE_FIRST_15_DAYS = new BigDecimal("0.005");    // 千分之五
    public static final BigDecimal RATE_15_TO_30_DAYS = new BigDecimal("0.01");     // 百分之一
    public static final BigDecimal RATE_OVER_30_DAYS = new BigDecimal("0.02");      // 百分之二

    /**
     * 违章记录状态
     */
    public static final String UNPROCESSED = "violationStatus-1";   // 未处理
    public static final String PROCESSED = "violationStatus-2";     // 已处理

    /**
     * 罚单状态
     */
    public static final String UNPAID = "ticketStatus-1";   // 未缴费
    public static final String PARTIALLY_PAID = "ticketStatus-2";   // 部分缴清
    public static final String FULLY_PAID = "ticketStatus-3";   // 已缴清

    /**
     * 角色类型
     */
    public static final String VEHICLE_OWNER = "vehicleOwner";
    public static final Long VEHICLE_OWNER_ID = 1L;
    public static final String COMMON_ADMIN = "commonAdmin";
    public static final Long COMMON_ADMIN_ID = 2L;

    /**
     * 字典表类别
     */
    public static final String VEHICLE_TYPE = "vehicleType";
    public static final String VEHICLE_COLOR = "vehicleColor";
    public static final String VEHICLE_BRAND = "vehicleBrand";
    public static final String VIOLATION_STATUS = "violationStatus";
    public static final String TICKET_STATUS = "ticketStatus";

    /**
     * 正则表达式
     */
    public static final String USERNAME_REGEX = "^[\\u4e00-\\u9fa5]{2,}$";
    public static final String ID_Regex = "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]$";
    public static final String DL_NUMBER_REGEX = "^\\d{12}$";
    public static final String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@(.+)$";
    public static final String MOBILE_REGEX = "^1[3-9]\\d{9}$";
    public static final String PLATE_NUMBER_REGEX = "^[\\u4e00-\\u9fa5][A-Z][A-Z0-9]{5}$";
    public static final String VIN_REGEX = "^[A-HJ-NPR-Z0-9]{17}$";
    public static final String FINE_REGEX = "^\\d+$";
    public static final String SORT_VALUE_REGEX = "^[1-9]\\d*$";

    /**
     * 菜单类型
     *
     * @author chenshun
     * @email sunlightcs@gmail.com
     * @date 2016年11月15日 下午1:24:29
     */
    public enum MenuType {
        /**
         * 目录
         */
        CATALOG(0),
        /**
         * 菜单
         */
        MENU(1),
        /**
         * 按钮
         */
        BUTTON(2);

        private int value;

        MenuType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    /**
     * 定时任务状态
     *
     * @author chenshun
     * @email sunlightcs@gmail.com
     * @date 2016年12月3日 上午12:07:22
     */
    public enum ScheduleStatus {
        /**
         * 正常
         */
        NORMAL(0),
        /**
         * 暂停
         */
        PAUSE(1);

        private int value;

        ScheduleStatus(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    /**
     * 云服务商
     */
    public enum CloudService {
        /**
         * 七牛云
         */
        QINIU(1, QiniuGroup.class),
        /**
         * 阿里云
         */
        ALIYUN(2, AliyunGroup.class),
        /**
         * 腾讯云
         */
        QCLOUD(3, QcloudGroup.class);

        private int value;

        private Class<?> validatorGroupClass;

        CloudService(int value, Class<?> validatorGroupClass) {
            this.value = value;
            this.validatorGroupClass = validatorGroupClass;
        }

        public int getValue() {
            return value;
        }

        public Class<?> getValidatorGroupClass() {
            return this.validatorGroupClass;
        }

        public static CloudService getByValue(Integer value) {
            Optional<CloudService> first = Stream.of(CloudService.values()).filter(cs -> value.equals(cs.value)).findFirst();
            if (!first.isPresent()) {
                throw new IllegalArgumentException("非法的枚举值:" + value);
            }
            return first.get();
        }
    }

}
